package cn.com.self.apis.portal.service.impl;

import cn.com.self.apis.portal.entity.SysRoleEntity;
import cn.com.self.apis.portal.mapper.SysRoleMapper;
import cn.com.self.apis.portal.service.intf.ISysRoleService;
import cn.com.self.apis.portal.util.ICustomPage;
import cn.com.self.apis.portal.vo.TreeNodeModel;
import cn.com.self.apis.portal.vo.TreeNodesModel;
import cn.com.selfsoft.smartcity.core.service.BaseServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.thymeleaf.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
* <p>
    * 系统角色 服务实现类
    * </p>
*
* @author chenyongren
* @since 2019-09-27
*/
@Service
public class SysRoleServiceImpl extends BaseServiceImpl<SysRoleEntity, String> implements ISysRoleService {

    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Override
    public int deletes(List<Long> ids) {
        return 0;
    }

    @Autowired


     /**构造角色树
      * @Description //TODO
      * @return
      **/
    @Override
    public TreeNodesModel createRoleTree(Map<String, Object> queryMap) {
        QueryWrapper<SysRoleEntity> queryWrapper = new QueryWrapper<>();
        TreeNodesModel result = new TreeNodesModel();
        //获取出所有的角色
        List<SysRoleEntity> list  = new ArrayList<>();
        list = sysRoleMapper.selectList(queryWrapper);
        int total = list.size();
        //对角色进行树形构造
        List<TreeNodeModel> roleList = new ArrayList<TreeNodeModel>();
        for (int i =0;i<total;i++){
            SysRoleEntity info = list.get(i);
            TreeNodeModel treeNode = new TreeNodeModel();
            treeNode.setCode(info.getCode());
            treeNode.setId(String.valueOf(info.getId()));
            treeNode.setName(info.getName());
            treeNode.setPId(info.getParent());
            roleList.add(treeNode);
        }

        result.setDatas(roleList);
        result.setTotal(total);
        return result;

    }

    @Override
    public ICustomPage<SysRoleEntity> selectPage(Map<String, Object> queryParams, Page pageable) {
        QueryWrapper<SysRoleEntity> queryWrapper = new QueryWrapper<>();
        queryParams.keySet().forEach(key -> {
            if(("id").equals(key)){
                queryWrapper.eq("id", queryParams.get("id"));
            }else if("code".equals(key)) {
                queryWrapper.eq("code",queryParams.get("code"));
            }else if("name".equals(key)) {
                queryWrapper.like("name",queryParams.get("name"));
            }else if("parent".equals(key)) {
                queryWrapper.eq("parent",queryParams.get("parent")).or().eq("id", queryParams.get("parent"));

            }
        });
        queryWrapper.orderByDesc("id");
       ICustomPage<SysRoleEntity> pageList =  (ICustomPage<SysRoleEntity>)sysRoleMapper.selectPage(pageable,queryWrapper);
        return  pageList;
    }

    @Override
    public List<SysRoleEntity> selectRoleList(Map<String, Object> queryParams) {
        String admin = (String) queryParams.get("is_admin");
        if(!StringUtils.isEmpty(admin) && admin.equals("1")){
            QueryWrapper<SysRoleEntity> queryWrapper = new QueryWrapper<>();
            return this.sysRoleMapper.selectList(queryWrapper);
        }
        return sysRoleMapper.selectRoleList(queryParams);
    }

    /*@Override
    public PageImpl<SysRoleEntity> selectRoleList(Map<String, Object> queryParams, Pageable page) {

        Integer total = sysRoleMapper.selectRoleListTotal(queryParams);
        if(total==null){
            total = 0;
        }
        queryParams.put("page", (page.getPageNumber()- 1) * page.getPageSize());
        queryParams.put("size", page.getPageSize());
        //List<OldExchangeNewVo> list = oldExchangeNewMapper.selectPages(parameterMap);
        List<SysRoleEntity> pageList =  sysRoleMapper.selectRoleList(queryParams);
        return new PageImpl(pageList, PageRequest.of(page.getPageNumber() - 1, page.getPageSize()), total);
    }
   */

}
